/**
 * Access (ABM), 2017-2020, 单创.
 */
package com.tengju.bff.interfaces.covercharge.service.message.client;

import com.alibaba.fastjson.JSONObject;
import com.idanchuang.component.base.JsonResult;
import com.idanchuang.component.config.apollo.util.ConfigUtil;
import com.tengju.bff.interfaces.covercharge.service.message.code.MessageService;
import com.tengju.bff.interfaces.covercharge.util.UserUtils;
import com.idanchuang.service.base.sms.api.service.ISmsServiceProvider;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.model.user.UserInfo;
import com.tengju.user.domain.model.user.UserInfoRepository;
import com.zoi7.component.core.exception.ServiceException;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.implementation.bytecode.Throw;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TTransport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author WenLei 2020年3月19日.
 */
@Slf4j
@Service
public class UserMessageClient {

    @Resource
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private ISmsServiceProvider smsServiceProvider;

    @Autowired
    private UserInfoRepository userInfoRepository;

    private MessageService.Client client;

    private TProtocol tProtocol;

    private TTransport tTransport;

    @Setter
    private String host;

    @Setter
    private int port;

    @Setter
    private String serviceName;


    /**
     * 给ABM app发送站内信和push
     *  @param uid 用户idCode
     * @param title 消息标题
     * @param content  消息内容
     * @param isJumper 消息是否可跳转内页查看
     * @param isSend 是否发送站内信
     * @param isJpush 是否极光推送 1是 0否
     */
    public void sendDtServiceFeeMessage(Integer uid, String title, String content, Boolean isJumper, Boolean isSend, Boolean isJpush)  {
        threadPoolExecutor.execute(() -> {
            try {
                System.out.println("sendDtServiceFeeMessage");
                sendMessage(uid, title, content, 8, isJumper ? 2 : 1, isSend ? 1  : 0, "DT", isJpush ? 1 : 0);
            } catch (IOException e) {
                log.error("sendDtServiceFeeMessage 站内信发送失败", e);
            }
        });
    }

    /**
     * 发送短信
     *
     * @param idCode  用户Idcode
     * @param text        内容
     */
//    public void sendMobileMessage(Integer idCode, String text)  {
//        threadPoolExecutor.execute(() -> {
//            try {
//                UserInfo userInfo = userInfoRepository.getByIdCode(new UserIdCode(idCode));
//                if(userInfo != null){
//                   smsServiceProvider.sendText(userInfo.getMobile(),text);
//                }
//            } catch (Exception e) {
//                log.error("发送手机通知短信失败 error idCode="+idCode+",text="+text, e);
//            }
//        });
//    }

    /**
     * 指定模板发送短信
     *
     * @param idCode  用户Idcode
     * @param templateId  模板
     * @param values 内容
     */
//    public void sendMobileMessageByTemplate(Integer idCode, Integer templateId,String values)  {
//        threadPoolExecutor.execute(() -> {
//            try {
//                UserInfo userInfo = userInfoRepository.getByIdCode(new UserIdCode(idCode));
//                if(userInfo != null){
//                    smsServiceProvider.sendTemplate(userInfo.getMobile(),templateId,values);
//                }
//            } catch (Exception e) {
//                log.error("发送手机通知短信失败 error idCode="+idCode+",templateId="+templateId+"text="+values+"info="+e);
//            }
//        });
//    }

    /**
     * 通过手机号发送短信
     *
     * @param mobile  手机号
     * @param templateId  模板
     * @param values 内容
     */
//    public void sendMobileMessageByTemplateWithMobile(String mobile, Integer templateId,String values) {
//        threadPoolExecutor.execute(() -> {
//            try {
//                smsServiceProvider.sendText(mobile, values);
//            } catch (Exception e) {
//                log.error("发送手机通知短信失败 error mobile=" + mobile + ",templateId=" + templateId + "text=" + values + "info=" + e);
//            }
//        });
//    }

    /**
     * 违约金指定模板发送短信
     *
     * @param idCode  用户Idcode
     * @param templateId  模板
     * @param values 内容
     */
    public void rewardSendMobileMessageByTemplate(Integer idCode, Integer templateId,String values)  {
        try {
            UserInfo userInfo = userInfoRepository.getByIdCode(new UserIdCode(idCode));
            log.info("[违约金][后台管理发送短信入参]idCode{},templateId{},values{},userInfo{}",idCode,templateId,values,userInfo);
            if(userInfo != null){
                //违约金相关业务跟DT都没有关系
                //smsServiceProvider.sendTemplate(userInfo.getMobile(),templateId,values);
            }
        } catch (Exception e) {
            log.error("发送手机通知短信失败 error idCode="+idCode+",templateId="+templateId+"text="+values+"info="+e);
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 给指定用户发送站内消息
     *
     * @param uid 用户idCode
     * @param title 消息标题
     * @param content  消息内容
     * @param type  1: ABM公告|单创通知 2: 代理密信 3: 促销优惠 4: 交易物流 5: 培训消息 6: 私密消息 7: 新功能介绍
     * @param pushType 跳转方向 1: 不跳 2: 跳转自定义连接 3: 商品详情 4: 订单详情
     * @param isSend 是否发送消息 1发送 0不发送
     * @param platformCode 平台code
     * @param isJpush 是否极光推送 1是 0否
     */
    public String sendMessage(Integer uid, String title, String content, Integer type, Integer pushType, Integer isSend, String platformCode, Integer isJpush) throws IOException {
        HttpPostUtils httpPost = new HttpPostUtils();
        JSONObject sendJson = new JSONObject();

        sendJson.put("uid", uid);
        sendJson.put("title", title);
        sendJson.put("content", content);
        sendJson.put("type", type);
        sendJson.put("pushType", pushType);
        sendJson.put("isSend", isSend);
        sendJson.put("code", platformCode);
        sendJson.put("isJpush", isJpush);


        sendJson.put("pushTime", "");
        sendJson.put("extras", "");
        sendJson.put("titleImg", "");
        sendJson.put("template", new JSONObject());

        String jsonStr = JSONObject.toJSONString(sendJson);
        String url = ConfigUtil.getConfig().getString("message-center.host", "");
        return  httpPost.postJson(
                url + "/sendMessage",
                jsonStr, 90000, 90000, 90000);
    }
}
